<template>
	<form-container title="工位编辑" :width="700" :visible="visible" :destroy-on-close="true" @close="onClose">
		<a-form ref="formRef" :model="formState" :rules="formRules" layout="vertical">
			<a-form-item label="工位编码：" name="code">
				<a-input v-model:value="formState.code" placeholder="请输入工位编码" allow-clear />
			</a-form-item>
			<a-form-item label="工位名称：" name="name">
				<a-input v-model:value="formState.name" placeholder="请输入工位名称" allow-clear />
			</a-form-item>
			<a-form-item label="描述：" name="description">
				<a-textarea v-model:value="formState.description" placeholder="请输入描述" allow-clear />
			</a-form-item>
			<a-form-item label="产线名称：" name="line_id">
				<a-select
					v-model:value="formState.line_id"
					placeholder="请选择所产线"
					:options="lineOptions"
					:loading="lineLoading"
					allow-clear
				/>
			</a-form-item>
			<a-form-item label="工序名称：" name="process_operation_id">
				<a-select
					v-model:value="formState.process_operation_id"
					placeholder="请选择工序"
					:options="processOperationOptions"
					:loading="processOperationLoading"
					allow-clear
				/>
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmitworkstation">保存</a-button>
		</template>
	</form-container>
</template>

<script setup name="updatePassword">
	import { required } from '@/utils/formRules'
	import workstationApi from '@/api/workshop/workstationApi.js'
	import { onMounted, ref } from 'vue'
	import lineApi from '@/api/workshop/lineApi.js' // 假设有产线API
	import ProcessOperationApi from '@/api/dev/ProcessOperationApi.js' // 假设有工序API
	// 定义emit事件
	const emit = defineEmits({ successful: null })
	// 默认是关闭状态
	const visible = ref(false)
	const formRef = ref()
	// 表单数据
	const formState = ref({})
	const submitLoading = ref(false)

	// 产线下拉选项
	const lineOptions = ref([])
	const lineLoading = ref(false)

	// 工序下拉选项
	const processOperationOptions = ref([])
	const processOperationLoading = ref(false)

	// 获取工序列表
	const ProcessOperationList = async () => {
		try {
			processOperationLoading.value = true
			const res = await ProcessOperationApi.processOperationPage() // 假设API有list方法获取工序列表
			processOperationOptions.value = res.records.map((item) => ({
				label: item.name,
				value: item.id
			}))
		} catch (error) {
			console.error('获取工序列表失败', error)
		} finally {
			processOperationLoading.value = false
		}
	}
	// 获取产线列表
	const getlinelist = async () => {
		try {
			lineLoading.value = true
			const res = await lineApi.linelist() // 假设API有list方法获取车间列表
			lineOptions.value = res.records.map((item) => ({
				label: item.name,
				value: item.id
			}))
		} catch (error) {
			console.error('获取产线列表失败', error)
		} finally {
			lineLoading.value = false
		}
	}

	// 在组件挂载时获取车间列表
	onMounted(() => {
		ProcessOperationList()
		getlinelist()
	})

	// 打开抽屉
	const onOpen = (email) => {
		visible.value = true
		formState.value = email
	}
	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}
	// 表单校验规则
	const formRules = {
		code: [required('请输入产线编码')],
		name: [required('请输入产线名称')],
		workshopId: [required('请选择所属车间')]
	}

	// 提交数据
	const onSubmitworkstation = async () => {
		try {
			await formRef.value.validateFields()
			submitLoading.value = true
			const formData = {
				id: formState.value.id,
				code: formState.value.code,
				name: formState.value.name,
				line_id: formState.value.line_id,
				process_operation_id: formState.value.process_operation_id,
				description: formState.value.description
			}
			// 提交数据
			await workstationApi.workstationedit(formData)
			formRef.value.resetFields()
			visible.value = false
			emit('successful', formData)
		} catch (error) {
			console.error('提交失败', error)
		} finally {
			submitLoading.value = false
		}
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style scoped></style>
